package com.gvitz.transaction.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.gvitz.transaction.R;
import com.gvitz.transaction.utils.Config;


public class SplashActivity extends GVActivity {
	
	private static final int ABOUT = 3;
	SharedPreferences prefs= null;
		

	/** Activity para mostrar la imagen de Bienvenida */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
	     super.onCreate(savedInstanceState);		
		 setContentView(R.layout.activity_splash); 

		    // Estableciendo parametros por defecto	
	    	
	        prefs =  getSharedPreferences("preferencias", Context.MODE_PRIVATE);
	        final SharedPreferences.Editor editor = prefs.edit();	
              
	        new Handler().postDelayed(new Runnable() {
				
					  @Override
			          public void run() { 
						  
						  // revisar si se ha iniciado la aplicacion
						  if ((prefs.getString("app_block","nuevo").equals("nuevo"))||(prefs.getString("app_block","").equals("0"))){
								
							  //entrar por primera vez o no
							  if (prefs.getString("first_time","").equals("2")){
									  
								  Config.updateInfoClientes(SplashActivity.this,getApplicationContext()); 									    
								       new Handler().postDelayed(new Runnable() {
											
											  @Override
									          public void run() { 
									            	Intent mainIntent = new Intent(SplashActivity.this, PingActivity.class);
									                SplashActivity.this.startActivity(mainIntent);
									                overridePendingTransition (R.anim.open_next, R.anim.close_main);
									                SplashActivity.this.finish();
									                
											  }},2000);
								  	
								  }else{
			        	
								        if ((prefs.getString("encode_usuario","nuevo").equals("nuevo"))
								        		&&(prefs.getString("id_usuario","nuevo").equals("nuevo"))
								        		&&(prefs.getString("pin_usuario","nuevo").equals("nuevo"))	
								        		&&(prefs.getString("app_block","nuevo").equals("nuevo"))
								        		&&(prefs.getString("count_block","nuevo").equals("nuevo"))
								        		&&(prefs.getString("latitud","nuevo").equals("nuevo"))
								        		&&(prefs.getString("longitud","nuevo").equals("nuevo"))
								        		&&(prefs.getString("fecha_actualizacion","nuevo").equals("nuevo"))								        								        		
								        		&&(prefs.getString("first_time","nuevo").equals("nuevo"))){
								        	
								        	//setear variables por defecto
								   				 editor.clear();			 
								   				 editor.putString("fecha_actualizacion","2010-10-10 00:00:00");
								   				 editor.putString("pin_usuario","gvitz");
								   				 editor.putString("id_usuario","gvitz");
								   				 editor.putString("encode_usuario","gvitz");
								   				 editor.putString("first_time","1");
								   				 editor.putString("app_block","0");
								   				 editor.putString("count_block","0");
												 editor.putString("latitud","-31952162");
												 editor.putString("longitud","-71118166");
								   				 editor.commit();	
								   				 
								        		}
								        
								        
								        //abrir registro
						            	Intent mainIntent = new Intent(SplashActivity.this, RegisterActivity.class);
						                SplashActivity.this.startActivity(mainIntent);
						                overridePendingTransition (R.anim.open_next, R.anim.close_main);
						                SplashActivity.this.finish();
						                
								  	}
						  }
						  else{
							
							  // abrir actividad de bloqueo	
			                  Intent mainIntent = new Intent(SplashActivity.this, BlockedActivity.class);
					          SplashActivity.this.startActivity(mainIntent);
					          overridePendingTransition (R.anim.open_next, R.anim.close_main);
					          SplashActivity.this.finish(); 
		  
					          
						     }							  

					  }}, 1000);
	        		 	       
	}
		
    /** Se crear el menu con las distintas opciones.*/
	public boolean onCreateOptionsMenu(Menu menu) {
									
			menu.add(Menu.CATEGORY_ALTERNATIVE,ABOUT, Menu.NONE, "About")
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
			
			return true;
		}
 
	/** Funcion para ver que se realiza con cada accion del menu */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

			super.onOptionsItemSelected(item);
			
			if (item.getItemId() == ABOUT) {
				Intent i = new Intent(this, AboutActivity.class);
				startActivity(i);
			}
			
			return true;
		}
	
	@Override
	public void onBackPressed() {
		return;
	}
	

	 							
	
}
